package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("AdditiveaddressController")
public class AdditiveaddressController extends CmiEmaController {

	@RequestMapping("/listAdditiveaddresss")
	public ModelAndView listAdditiveaddresss(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = additiveaddressPaginationService.updatePagination(request, pagination);
		Set<Additiveaddress> additiveaddresss = additiveaddressPaginationService
				.getPaginationAdditiveaddresss(pagination);

		mav.addObject("additiveaddresss", additiveaddresss);
		mav.addObject("entityName", "additiveaddress");
		mav.addObject("pagination", pagination);
		mav.setViewName("additiveaddress/listAdditiveaddresss.jsp");
		return mav;
	}

	public String listAdditiveaddresss() {
		return "redirect:/listAdditiveaddresss";
	}

	@RequestMapping("/viewAdditiveaddress")
	public ModelAndView viewAdditiveaddress(@RequestParam Integer additiveaddressId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("additiveaddress", additiveaddressDAO.findAdditiveaddressByPrimaryKey(additiveaddressId));
		mav.setViewName("additiveaddress/viewAdditiveaddress.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteAdditiveaddresss")
	public ModelAndView confirmDeleteAdditiveaddresss(
			@RequestParam(value = "deleteAdditiveaddresssIds", required = true) Set<Integer> deleteAdditiveaddresssIds) {
		ModelAndView mav = new ModelAndView();

		Set<Additiveaddress> deleteAdditiveaddresss = additiveaddressDAO
				.findAdditiveaddresssByIds(deleteAdditiveaddresssIds);
		Set<Additiveaddress> referencedAdditiveaddresss = additiveaddressService
				.loadReferenceAdditiveaddresss(deleteAdditiveaddresss);
		boolean hasReferenced;
		if (referencedAdditiveaddresss.size() == 0) {
			mav.addObject("deleteEntitys", deleteAdditiveaddresss);
			hasReferenced = false;
		} else {
			deleteAdditiveaddresss.removeAll(referencedAdditiveaddresss);
			mav.addObject("referencedEntitys", referencedAdditiveaddresss);
			mav.addObject("deleteEntitys", deleteAdditiveaddresss);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Additiveaddress");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listAdditiveaddresss");
		return mav;
	}

	@RequestMapping("/deleteAdditiveaddress")
	public String deleteAdditiveaddress(@RequestParam Integer additiveaddressId) {
		Additiveaddress additiveaddress = additiveaddressDAO.findAdditiveaddressByPrimaryKey(additiveaddressId);
		additiveaddressService.deleteAdditiveaddress(additiveaddress);
		return "forward:/listAdditiveaddresss";
	}

	@RequestMapping("/confirmDeleteAdditiveaddress")
	public ModelAndView confirmDeleteAdditiveaddress(@RequestParam Integer additiveaddressId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("additiveaddress", additiveaddressDAO.findAdditiveaddressByPrimaryKey(additiveaddressId));

		mav.setViewName("additiveaddress/deleteAdditiveaddress.jsp");

		return mav;
	}

	@RequestMapping("/deleteAdditiveaddresss")
	public String deleteAdditiveaddresss(
			@RequestParam(value = "deleteAdditiveaddresssId", required = true) Set<Integer> deleteAdditiveaddresssId) {
		additiveaddressService.deleteAdditiveaddresss(deleteAdditiveaddresssId);
		return "forward:/listAdditiveaddresss";
	}

	@RequestMapping("/editAdditiveaddress")
	public ModelAndView editAdditiveaddress(@RequestParam Integer additiveaddressId) {

		ModelAndView mav = new ModelAndView();

		Additiveaddress additiveaddress = additiveaddressDAO.findAdditiveaddressByPrimaryKey(additiveaddressId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("additiveaddress", additiveaddress);

		// 把所有的引用additiveaddress对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(additiveaddressService.loadAdditiveaddressObjectReferencedSet(additiveaddress));

		// 将编辑页面additiveaddress添加到mav中
		mav.setViewName("additiveaddress/editAdditiveaddress.jsp");
		return mav;
	}

	@RequestMapping("/newAdditiveaddress")
	public ModelAndView newAdditiveaddress() {
		ModelAndView mav = new ModelAndView();
		Additiveaddress additiveaddress = new Additiveaddress();

		mav.addObject("additiveaddress", additiveaddress);

		// 把所有的引用additiveaddress对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(additiveaddressService.loadAdditiveaddressObjectReferencedSet(additiveaddress));

		mav.addObject("newFlag", true);
		mav.setViewName("additiveaddress/editAdditiveaddress.jsp");

		return mav;
	}

	@RequestMapping("/saveAdditiveaddress")
	public ModelAndView saveAdditiveaddress(@ModelAttribute Additiveaddress additiveaddress) {

		ModelAndView mav = new ModelAndView();

		Additive additiveTemp = additiveaddress.getAdditive();
		Additive additive = additiveTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(additiveDAO.findAdditiveById(additiveTemp.getId()));
		Address addressTemp = additiveaddress.getAddress();
		Address address = addressTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(addressDAO.findAddressById(addressTemp.getId()));
		Unit unitTemp = additiveaddress.getUnit();
		Unit unit = unitTemp == null ? null : CmiHibernateProxyUtil.deproxy(unitDAO.findUnitById(unitTemp.getId()));

		Additiveaddress existingAdditiveaddress = CmiHibernateProxyUtil.deproxy(additiveaddressDAO
				.findAdditiveaddressByPhysicalPrimaryKey(additive.getId(), address.getId(), additiveaddress.getTime()));

		additiveaddress.setAdditive(additive);
		additiveaddress.setAddress(address);
		additiveaddress.setUnit(unit);
		// 新建记录
		if (additiveaddress.getId() == null) {
			if (additiveaddress.equals(existingAdditiveaddress)) {

				mav.addAllObjects(additiveaddressService.loadAdditiveaddressClassReferencedSet(additiveaddress));
				// mav = editContinue(mav, additiveaddress);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("additiveaddress/editAdditiveaddress.jsp");
			} else {
				additiveaddressService.saveAdditiveaddress(additiveaddress);
				mav.setViewName("forward:/listAdditiveaddresss");
			}
		}
		// 编辑记录
		else {
			Additiveaddress originAdditiveaddress = CmiHibernateProxyUtil
					.deproxy(additiveaddressDAO.findAdditiveaddressByPrimaryKey(additiveaddress.getId()));
			if (!additiveaddress.equals(originAdditiveaddress) && additiveaddress.equals(existingAdditiveaddress)) {
				mav.addAllObjects(additiveaddressService.loadAdditiveaddressClassReferencedSet(additiveaddress));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("additiveaddress/editAdditiveaddress.jsp");
			} else {
				additiveaddressService.saveAdditiveaddress(additiveaddress);
				mav.setViewName("forward:/listAdditiveaddresss");
			}
		}
		return mav;
	}

	@RequestMapping("/additiveaddressController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
